package com.zzyl.framework.interceptor;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.zzyl.common.core.domain.model.LoginUser;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import lombok.SneakyThrows;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

//    // 插入数据时自动填充
//    @Override
//    public void insertFill(MetaObject metaObject) {
//        this.strictInsertFill(metaObject, "createBy", String.class, String.valueOf(getLoginUserId()));
//        this.strictInsertFill(metaObject, "createTime", Date.class, DateUtils.getNowDate());
//    }
//
//    // 修改数据时自动填充
//    @Override
//    public void updateFill(MetaObject metaObject) {
//        this.setFieldValByName("updateBy", String.valueOf(getLoginUserId()), metaObject);
//        this.setFieldValByName("updateTime", DateUtils.getNowDate(), metaObject);
////        this.strictUpdateFill(metaObject, "updateBy", String.class, String.valueOf(getLoginUserId()));
////        this.strictUpdateFill(metaObject, "updateTime", Date.class, DateUtils.getNowDate());
//    }
//
//    // 获取当前登录人的id
//    public Long getLoginUserId() {
//        LoginUser loginUser = SecurityUtils.getLoginUser();
//        if (ObjectUtils.isNotEmpty(loginUser)) {
//            return loginUser.getUserId();
//        }
//        return 1L;
//    }
//
//    private static Long loadUserId() {
//
//        //获取当前登录人的id
//        try {
//            LoginUser loginUser = SecurityUtils.getLoginUser();
//            if (ObjectUtils.isNotEmpty(loginUser)) {
//                return loginUser.getUserId();
//            }
//            return 1L;
//        } catch (Exception e) {
//            return 1L;
//        }
//    }


    @Autowired
    private HttpServletRequest request;

    @SneakyThrows
    public boolean isExclude() {
        try {
            String requestURI = request.getRequestURI();
            if(requestURI.startsWith("/member")){
                return true;
            }
            return false;
        } catch (Exception e) {
            return true;
        }
    }

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        if(!isExclude()) {
            this.strictInsertFill(metaObject, "createBy", String.class, getLoginUserId() + "");
        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime", new Date(), metaObject);
        if(!isExclude()) {
            this.setFieldValByName("updateBy", getLoginUserId() + "", metaObject);
        }

    }

    /**
     * 获取当前登录人的ID
     *
     * @return
     */
    private static Long getLoginUserId() {

        //获取当前登录人的id
        try {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            if (ObjectUtils.isNotEmpty(loginUser)) {
                return loginUser.getUserId();
            }
            return 1L;
        } catch (Exception e) {
            return 1L;
        }
    }

}
